<template>
  <el-dialog
    :visible.sync="showActiveDealerDialog"
    :show-close="false"
    :close-on-click-modal="false"
    width="80%"
    title="活动经销商选择"
    top="3vh"
  >
    <div class="searchform">

      <el-form ref="activeForm" :model="searchParams" size="mini" label-width="90px">
        <el-row :gutter="6">
          <el-col :xs="24" :sm="8" :md="6">
            <el-form-item label="服务站代码">
              <el-input v-model="searchParams.dealerCode" placeholder="服务站代码"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="8" :md="6">
            <el-form-item label="服务站名称">
              <el-input v-model="searchParams.dealerName" placeholder="服务站名称"/>
            </el-form-item>
          </el-col>
          <div style="text-align:right">
            <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
            <el-button icon="el-icon-refresh" @click="searchParams = {}">重置</el-button>
          </div>
        </el-row>
      </el-form>
    </div>
    <BaseTable
      ref="searchTable"
      height="200"
      @chaneg-size="changeSizeHandle"
      @chaneg-page="changePageHandle"
      @change-next="changeNext"
      @change-prev="changePrev"
      @selection-change="handleSelectionChange"
      @row-click="handelCurrentChange"
    >
      <el-table-column align="center" label="序号" type="index" min-width="10"/>
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column v-if="false" prop="DEALER_ID" align="center"/>
      <el-table-column prop="DEALER_CODE" align="center" label="服务站代码"/>
      <el-table-column prop="DEALER_NAME" align="center" label="服务站名称"/>
      <el-table-column prop="PROVINCE_NAME" align="center" label="省份"/>
      <el-table-column prop="CITY_NAME" align="center" label="城市"/>
    </BaseTable>
    <div slot="footer" class="dialog-footer" style="text-align:center">
      <el-button type="primary" icon="el-icon-document" @click="save">保存</el-button>
      <el-button icon="el-icon-back" @click="back">返回</el-button>
    </div>
  </el-dialog>
</template>

<script>
import rSelect from '@/components/Select/Select'
import BaseTable from '@/components/BaseTable'
import formMixins from '@/mixins/formMixins'
import { getAllDealer } from '@/api/repair/basedata/activity/activity'
export default {
  components: {
    rSelect,
    BaseTable
  },
  mixins: [formMixins],
  props: {
    showActiveDealerDialog: false,
    dealerData: Array
  },
  data() {
    return {
      search: {
        limit: 10,
        pageNum: 1
      },
      searchParams: {
        dealerCode: '',
        dealerName: ''
      },
      multipleSelection: []
    }
  },
  computed: {
    seriUrl() {
      if (this.searchParams.brand) {
        return `/vehicleProduct/brandsdict/${
          this.searchParams.brand
        }/seriessdict`
      }
    },
    modelUrl() {
      if (this.searchParams.brand && this.searchParams.series) {
        return `/vehicleProduct/brandsdict/${
          this.searchParams.brand
        }/seriessdict/${this.searchParams.series}/modelsdict`
      }
    }
  },
  methods: {
    searchHandle() {
      const table = this.$refs['searchTable']
      // 初始化
      for (const k in this.searchParams) {
        if (!this.searchParams[k]) {
          delete this.searchParams[k]
        }
      }
      const params = Object.assign({}, this.search, this.searchParams)
      getAllDealer(params).then(res => {
        table.wait()
        table.complete().filData(res)
      })
    },
    searchHandleBefore(productType) {
      this.searchParams.dealerCode = ''
      this.searchParams.dealerName = ''
      this.searchHandle()
    },
    save() {
      var checkFlag = true
      if (this.multipleSelection.length > 0) {
        for (let i = 0; i < this.multipleSelection.length; i++) {
          for (let j = 0; j < this.dealerData.length; j++) {
            if (this.multipleSelection[i].DEALER_CODE === this.dealerData[j].DEALER_CODE) {
              checkFlag = false
            }
          }
        }
        if (checkFlag) {
          this.$emit('selectRow5', this.multipleSelection)
          this.back()
        } else {
          this.$message({
            type: 'info',
            message: '请勿选择重复服务站'
          })
        }
      } else {
        this.$message({
          type: 'warning',
          message: '请选择一条数据'
        })
      }
    },
    back() {
      this.$refs.searchTable.$children[0].clearSelection()
      this.$emit('update:showActiveDealerDialog', false)
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handelCurrentChange(val) {
      if (val) {
        val.flag = !val.flag
        this.$refs.searchTable.$children[0].toggleRowSelection(val, val.flag)
      }
    }
  }
}
</script>

<style scoped>
</style>

